import { createRouter, createWebHistory } from 'vue-router'

import Home from '../views/Home.vue'
import store from '../store/index'
import noFind from '../comon/404.vue'

const routes = [
  // 重定向
  {
    path: '/',
    redirect: '/home',
  },

  // 登录 注册 忘记密码
  {
    path: '/login', //登录
    name: 'login',
    component: () => import('../comon/login.vue'),
  },
  {
    path: '/register', //注册
    name: 'register',
    component: () => import('../comon/register.vue'),
  },
  {
    path: '/forgetPassword', //忘记密码
    name: 'forgetPassword',
    component: () => import('../comon/forgetPassword.vue'),
  },
  {
    path: '/404', // 404 页面
    name: 'noFind',
    component: noFind,
  },
  // home首页面
  {
    path: '/home',
    redirect: '/home/index',
    name: 'home',
    component: Home,
    children: [
      // tabar 底部导航栏
      {
        path: 'index', //首页
        component: () => import('../views/index.vue'),
      },
      {
        path: 'notice', //公告
        component: () => import('../views/notice.vue'),
      },
      {
        path: 'my', //我的
        component: () => import('../views/my.vue'),
      },
      {
        path: 'sharelife', //趣事分享
        component: () => import('../components/index/more/sharelife.vue'),
      },
    ],
  },

  // 我的页面功能
  {
    path: '/myinfo', //查看个人信息
    component: () => import('../components/my/myinfo.vue'),
  },
  {
    path: '/myUpdate', //修改个人信息
    name: 'myUpdate',
    component: () => import('../components/my/myUpdate.vue'),
  },
  {
    path: '/myUpdatePassword', //修改个人密码
    name: 'myUpdatePassword',
    component: () => import('../components/my/myUpdatePassword.vue'),
  },
  {
    path: '/myUpdataEmail', //修改个人邮箱
    name: 'myUpdataEmail',
    component: () => import('../components/my/myUpdataEmail.vue'),
  },
  {
    path: '/myAllHistory', //个人历史记录
    name: 'myAllHistory',
    component: () => import('../components/my/myAllHistory.vue'),
  },
  {
    path: '/myUpdateHeader', //修改我的头像
    name: 'myUpdateHeader',
    component: () => import('../components/my/myUpdateHeader.vue'),
  },

  // 公告栏子页面
  {
    path: '/noticeOne/:id', //详细查看公告
    name: 'noticeOne',
    component: () => import('../components/notice/noticeOne.vue'),
  },
  {
    path: '/addNotic', //发布公告
    name: 'addNotic',
    component: () => import('../components/notice/addNotic.vue'),
  },

  {
    path: '/vote', //投票页面
    name: 'vote',
    component: () => import('../components/index/vote/vote.vue'),
  },
  {
    path: '/voteAdd', //增加投票
    name: 'voteAdd',
    component: () => import('../components/index/vote/voteAdd.vue'),
  },
  {
    path: '/voteShow', // 投票详情
    name: 'voteShow',
    component: () => import('../components/index/vote/voteShow.vue'),
  },

  {
    path: '/lostFound', //失物招领
    name: 'lostFound',
    component: () => import('../components/index/lostFound/lostFound.vue'),
  },
  {
    path: '/addLostFound', //添加失物招领
    name: 'addLostFound',
    component: () => import('../components/index/lostFound/addLostFound.vue'),
  },
  {
    path: '/detail/:id', //失物物品详细信息
    name: 'detail',
    component: () => import('../components/index/lostFound/detail.vue'),
  },

  {
    path: '/SearchNotice', //寻物启事
    name: 'SearchNotice',
    component: () =>
      import('../components/index/SearchNotice/SearchNotice.vue'),
  },
  {
    path: '/addFound', //添加寻物启事
    name: 'addFound',
    component: () => import('../components/index/SearchNotice/addFound.vue'),
  },
  {
    path: '/searchdetail/:id', //寻物启事详细信息
    name: 'searchdetail',
    component: () => import('../components/index/SearchNotice/detail.vue'),
  },

  {
    path: '/yield', //闲置转让
    name: 'yield',
    component: () => import('../components/index/yield/yield.vue'),
  },
  {
    path: '/addYield', //添加闲置转让
    name: 'addYield',
    component: () => import('../components/index/yield/addYieldFound.vue'),
  },
  {
    path: '/yieldDetail/:id', //闲置转让详细信息
    name: 'yieldDetail',
    component: () => import('../components/index/yield/detail.vue'),
  },

  {
    path: '/communityBorrow', //社区借用
    name: 'communityBorrow',
    component: () => import('../components/index/borrow/borrow.vue'),
  },

  {
    path: '/addBorrow', //添加社区借用
    name: 'addBorrow',
    component: () => import('../components/index/borrow/addBorrow.vue'),
  },
  {
    path: '/borrowDetail/:id/:type', //社区借用详细信息
    name: 'borrowDetail',
    component: () => import('../components/index/borrow/detail.vue'),
  },

  {
    path: '/teamShopping', //社区团购
    name: 'teamShopping',
    component: () =>
      import('../components/index/teamShopping/teamShopping.vue'), //
  },
  {
    path: '/ShoppingInfo/:id', //物品详情
    name: 'ShoppingInfo',
    component: () =>
      import('../components/index/teamShopping/ShoppingInfo.vue'), //
  },
  {
    path: '/ShoppingDetail/:id', //购物记录
    name: 'ShoppingDetail',
    component: () => import('../components/index/teamShopping/payMoney.vue'), //
  },

  {
    path: '/propertyPayment', //物业缴费
    name: 'propertyPayment',
    component: () =>
      import('../components/index/propertyPayment/propertyPayment.vue'),
  },

  {
    path: '/poperHstory', //物业缴费记录
    name: 'poperHstory',
    component: () =>
      import('../components/index/propertyPayment/poperHstory.vue'),
  },

  {
    path: '/repair', //物业报修
    name: 'repair',
    component: () => import('../components/index/repair/repair.vue'),
  },
  {
    path: '/repair_History', //物业报修记录
    name: 'repair_History',
    component: () => import('../components/index/repair/repair_History.vue'),
  },

  {
    path: '/door', //智慧门禁
    name: 'door',
    component: () => import('../components/index/door/door.vue'),
  },
  {
    path: '/authentication', //社区认证页面
    name: 'authentication',
    component: () => import('../components/community/authentication.vue'),
  },

  {
    path: '/stores', //社区店铺
    name: 'stores',
    component: () => import('../components/index/store/store.vue'),
  },
  {
    path: '/addStores', //添加社区店铺
    name: 'addStores',
    component: () => import('../components/index/store/addStoreFound.vue'),
  },
  {
    path: '/storesDetail/:id/:title', //社区店铺详情
    name: 'storesDetail',
    component: () => import('../components/index/store/detail.vue'),
  },

  {
    path: '/complaint', //物业投诉
    name: 'complaint',
    component: () => import('../components/index/complaint/complaint.vue'), //complaintHistory
  },
  {
    path: '/complaintHistory', //物业投诉的历史记录
    name: 'complaintHistory',
    component: () =>
      import('../components/index/complaint/complaintHistory.vue'),
  },

  {
    path: '/monitor', //社区监控
    name: 'monitor',
    component: () => import('../components/index/monitor/monitor.vue'), //
  },
  {
    path: '/monitorHstory', //社区监控
    name: 'monitorHstory',
    component: () => import('../components/index/monitor/monitorHstory.vue'), //
  },

  {
    path: '/AboutUs', //关于我们
    name: 'AboutUs',
    component: () => import('../components/AboutUs/AboutUs.vue'),
  },

  {
    path: '/wisdomalertor', //更多 智能报警
    name: 'wisdomalertor',
    component: () => import('../components/index/more/wisdomAlertor.vue'),
  },
  {
    path: '/wisdomdoor', //更多 智能门卫
    name: 'wisdomDoor',
    component: () => import('../components/index/more/wisdomDoor.vue'),
  },
  {
    path: '/shareLifeDetails/:id', //更多 分享生活的详情页面
    name: 'shareLifeDetails',
    component: () => import('../comon/shareLifeDetails.vue'),
  },
  {
    path: '/addSharelife', //更多 添加分享生活页面
    name: 'addSharelife',
    component: () => import('../components/index/more/addSharelife.vue'),
  },

  {
    path: '/news', //更多 热点新闻详情页面
    name: 'news',
    component: () => import('../components/index/news.vue'),
  },

  {
    path: '/newsDetail/:id', //热点新闻详情页面
    name: 'newsDetail',
    component: () => import('../components/index/newsDetail.vue'),
  },

  {
    path: '/hotNews/:active', //更多 热点新闻详情页面
    name: 'hotNews',
    component: () => import('../components/index/more/hotNews.vue'),
  },

  {
    path: '/hotNewsDetail/:id/:active', //热点新闻详情页面
    name: 'hotNewsDetail',
    component: () => import('../components/index/more/hotNewsDetail.vue'),
  },

  {
    path: '/aBeautifulRticle', //随机美文
    name: 'aBeautifulRticle',
    component: () => import('../components/index/more/aBeautifulRticle.vue'),
  },
  {
    path: '/weather', //出行指南
    name: 'weather',
    component: () => import('../components/index/more/weather.vue'),
  },
  {
    path: '/LickTheDog', //娱乐
    name: 'LickTheDog',
    component: () => import('../components/index/more/LickTheDog.vue'),
  },

  {
    path: '/:pathMatch(.*)',
    redirect: '/404',
  },
]

////////////// 路由守卫

const router = createRouter({
  history: createWebHistory(),
  // mode:'history',
  routes,
})

// 路由守卫

router.beforeEach((to, from, next) => {
  // 每次跳转保存路由路径
  // l路由守卫中是获取不到this 的 应为实例未创建
  store.commit('SET_Router_Path', to.path)
  let refused = [
    '/voteShow',
    '/home/my',
    '/voteAdd',
    '/addLostFound',
    '/addFound',
    '/addYield',
    '/addBorrow',
    '/repair',
    '/complaint',
    '/propertyPayment',
    '/addNotic',
    '/ShoppingInfo/**',
  ]
  let refused2 = ['/userInfo', '/modificatonPassword']

  // 判读是否访问权限路径
  if (refused.includes(to.path)) {
    // 获取登录令牌
    const tokenStr = window.localStorage.getItem('token')
    //判断是否登录
    if (!tokenStr) {
      // 未登录访问权限路径 跳转登录页面
      return next('/login')
    } else {
      // 登录访问普通记录路径 放行
      next()
    }
  } else {
    // 访问普通路径 登录或不登录都可以访问 放行
    next()
  }
})

router.afterEach((to, from) => {
  const toDepth = to.path.split('/').length
  const fromDepth = from.path.split('/').length
  to.meta.transitionName = toDepth < fromDepth ? 'slide-right' : 'slide-left'
})

export default router
